/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.esiag.servlets;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author assia
 */
public class DateUtil {

    public static String dateToString(Date d) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(d);
    }

    public static Date stringToDate(String Date) {
        Date d = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            d = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
public static Date formater(String datesemaine) {

     Date d = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            d = formatter.parse(datesemaine);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
    public static Date mondayOfWeek(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);
        
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        System.out.println("the monday of week is " + cal.getTime());
        return cal.getTime();
    }

    public static Date saturdayOfWeek(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        System.out.println("the saturady of week is " + cal.getTime());
        return cal.getTime();
    }

    public static int numberOfWeek(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);
        cal.get(Calendar.WEEK_OF_YEAR);
        // cal.set(Calendar.DAY_OF_WEEK, Calendar.WEEK_OF_YEAR);
        System.out.println("the number off of week is " + cal.get(Calendar.WEEK_OF_YEAR));
        return cal.get(Calendar.WEEK_OF_YEAR);


    }

    public static Date nextWeek(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // Puis on ajoute 7 jours :
        cal.add(Calendar.DATE, 7);
        System.out.println("the next manday is " + cal.getTime());
        return cal.getTime();
    }

    public static Date mardi(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        // Puis on ajoute 7 jours :
        System.out.println("tuesday is " + cal.getTime());
        return cal.getTime();
    }

    public static Date mercredi(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
        // Puis on ajoute 7 jours :
        System.out.println("merdredi is " + cal.getTime());
        return cal.getTime();
    }

    public static Date jeudi(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
        // Puis on ajoute 7 jours :
        System.out.println("jeudi is " + cal.getTime());
        return cal.getTime();
    }

    public static Date vendredi(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        // Puis on ajoute 7 jours :
        System.out.println("vendredi is " + cal.getTime());
        return cal.getTime();
    }

    public static Date samedi(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        // Puis on ajoute 7 jours :
        System.out.println("samedi is " + cal.getTime());
        return cal.getTime();
    }

    public static Date lastWeek(Date datesemaine) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(datesemaine);

        // On se positionne sur le Lundi de la semaine courante :

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // Puis on diminue 7 jours :
        cal.add(Calendar.DATE, -7);
        System.out.println("the last monday is " + cal.getTime());
        return cal.getTime();
    }
}
